package org.toools.gwt.demo.server;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.NoResultException;
import javax.persistence.Persistence;
import net.sf.gilead.gwt.PersistentRemoteService;
import org.toools.gwt.demo.client.DemoService;
import org.toools.gwt.demo.jpa.Account;
import org.toools.gwt.demo.jpa.Additive;
import org.toools.gwt.demo.jpa.Application;
import org.toools.gwt.demo.jpa.Company;
import org.toools.gwt.demo.jpa.CompanyPart;
import org.toools.gwt.demo.jpa.Container;
import org.toools.gwt.demo.jpa.Disposer;
import org.toools.gwt.demo.jpa.Waste;
import org.toools.gwt.demo.server.dao.IAccountDAO;
import org.toools.gwt.demo.server.dao.jpa.AcountDAO;

public class DemoServiceImpl extends PersistentRemoteService implements DemoService {

    private final static EntityManagerFactory EMF = Persistence.createEntityManagerFactory("tooolsPU");

    private IAccountDAO accountdao;

    public DemoServiceImpl() {
        //TODO get with swing
        accountdao = new AcountDAO();
    }

    public String test() {
        return "geeeht!";
    }

    public Account getAccountByID(Integer ID) {
        EntityManager em = EMF.createEntityManager();
        Account result;
        try {
            result = (Account) em.createNamedQuery("Account.getById").setParameter("id", ID).getSingleResult();
        } catch (NoResultException e) {
            return null;
        }
        System.out.println("fetched account " + result.getId() + " from db.");
        return result;
    }

    public List<Account> getAccounts() {
        EntityManager em = EMF.createEntityManager();
        List<Account> resultList = em.createNamedQuery("Account.getAll").getResultList();
        System.out.println("fetched " + resultList.size() + " accounts from db.");
        return resultList;
    }

    public Additive getAdditiveByID(Integer ID) {
        EntityManager em = EMF.createEntityManager();
        Additive result;
        try {
            result = (Additive) em.createNamedQuery("Additive.getById").setParameter("id", ID).getSingleResult();
        } catch (NoResultException e) {
            return null;
        }
        System.out.println("fetched additive " + result.getId() + " from db.");
        return result;
    }

    public List<Additive> getAdditives() {
        EntityManager em = EMF.createEntityManager();
        List<Additive> resultList = em.createNamedQuery("Additive.getAll").getResultList();
        System.out.println("fetched " + resultList.size() + " additives from db.");
        return resultList;
    }

    public Application getApplicationByID(Integer ID) {
        EntityManager em = EMF.createEntityManager();
        Application result;
        try {
            result = (Application) em.createNamedQuery("Application.getById").setParameter("id", ID).getSingleResult();
        } catch (NoResultException e) {
            return null;
        }
        System.out.println("fetched application " + result.getId() + " from db.");
        return result;
    }

    public List<Application> getApplications() {
        EntityManager em = EMF.createEntityManager();
        List<Application> resultList = em.createNamedQuery("Application.getAll").getResultList();
        System.out.println("fetched " + resultList.size() + " applications from db.");
        return resultList;
    }

    public Company getCompanyByID(Integer ID) {
        EntityManager em = EMF.createEntityManager();
        Company result;
        try {
            result = (Company) em.createNamedQuery("Company.getById").setParameter("id", ID).getSingleResult();
        } catch (NoResultException e) {
            return null;
        }
        System.out.println("fetched company " + result.getId() + " from db.");
        return result;
    }

    public List<Company> getCompanies() {
        EntityManager em = EMF.createEntityManager();
        List<Company> resultList = em.createNamedQuery("Company.getAll").getResultList();
        System.out.println("fetched " + resultList.size() + " companies from db.");
        return resultList;
    }

    public CompanyPart getCompanyPartByID(Integer ID) {
        EntityManager em = EMF.createEntityManager();
        CompanyPart result;
        try {
            result = (CompanyPart) em.createNamedQuery("CompanyPart.getById").setParameter("id", ID).getSingleResult();
        } catch (NoResultException e) {
            return null;
        }
        System.out.println("fetched company part " + result.getId() + " from db.");
        return result;
    }

    public List<CompanyPart> getCompanyParts() {
        EntityManager em = EMF.createEntityManager();
        List<CompanyPart> resultList = em.createNamedQuery("CompanyPart.getAll").getResultList();
        System.out.println("fetched " + resultList.size() + " companyparts from db.");
        return resultList;
    }

    public Container getContainerByID(Integer ID) {
        EntityManager em = EMF.createEntityManager();
        Container result;
        try {
            result = (Container) em.createNamedQuery("Container.getById").setParameter("id", ID).getSingleResult();
        } catch (NoResultException e) {
            return null;
        }
        System.out.println("fetched container " + result.getId() + " from db.");
        return result;
    }

    public List<Container> getContainers() {
        EntityManager em = EMF.createEntityManager();
        List<Container> resultList = em.createNamedQuery("Container.getAll").getResultList();
        System.out.println("fetched " + resultList.size() + " conainers from db.");
        return resultList;
    }

    public Disposer getDisposerByID(Integer ID) {
        EntityManager em = EMF.createEntityManager();
        Disposer result;
        try {
            result = (Disposer) em.createNamedQuery("Disposer.getById").setParameter("id", ID).getSingleResult();
        } catch (NoResultException e) {
            return null;
        }
        System.out.println("fetched disposer " + result.getId() + " from db.");
        return result;
    }

    public List<Disposer> getDisposers() {
        EntityManager em = EMF.createEntityManager();
        List<Disposer> resultList = em.createNamedQuery("Disposer.getAll").getResultList();
        System.out.println("fetched " + resultList.size() + " disposers from db.");
        return resultList;
    }

    public Waste getWasteByID(Integer ID) {
        EntityManager em = EMF.createEntityManager();
        Waste result;
        try {
            result = (Waste) em.createNamedQuery("Waste.getById").setParameter("id", ID).getSingleResult();
        } catch (NoResultException e) {
            return null;
        }
        System.out.println("fetched waste " + result.getGtin() + " from db.");
        return result;
    }

    public List<Waste> getWaste() {
        EntityManager em = EMF.createEntityManager();
        List<Waste> resultList = em.createNamedQuery("Waste.getAll").getResultList();
        System.out.println("fetched " + resultList.size() + " wastes from db.");
        return resultList;
    }

    public void create(Object object) {
        EntityManager em = EMF.createEntityManager();
        em.getTransaction().begin();
        em.persist(object);
        em.flush();
        em.getTransaction().commit();
        em.close();
    }

    public void update(Object object) {
        EntityManager em = EMF.createEntityManager();
        em.getTransaction().begin();
        em.merge(object);
        em.flush();
        em.getTransaction().commit();
        em.close();
    }

    public void delete(Object object) {
        EntityManager em = EMF.createEntityManager();
        em.getTransaction().begin();
        em.remove(em.merge(object));
        em.flush();
        em.getTransaction().commit();
        em.close();
    }
}